<?php
// +----------------------------------------------------------------------
// | Fengli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yanyongwang.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 风黎 < 1280246895@qq.com >
// +----------------------------------------------------------------------


namespace app\api\controller;


use think\Request;

/**
 * 用户接口控制器
 * Class Base
 * @package app\admin\controller
 */
class Users extends Base
{
    /**
     * 微信openid获取用户信息
     */
    public function wxGetUserInfo()
    {
        if(!request()->has('openid'))   return $this->returnData('400','openid不能为空');
        $user               = model('users')->where(['openid'=>request()->param('openid')])->find();
        if($user){
            return $this->returnData('200','请求成功',['openid'=>$user->openid,'nickname'=>$user->nickname,'head_url'=>$user->head_url]);
        }else{
            return $this->returnData('400','没有该用户');
        }
    }
    /**
     * 微信登录
     */
    public function wxLogin()
    {
        $appid          = "wx9883df9ffef003c0";
        $redirect_uri    = urlencode('http://'.request()->server('HTTP_HOST') . url('api/users/wxInfo'));
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        header("Location:" . $url);
    }
    /**
     * 微信code获取用户信息
     */
    public function wxInfo()
    {
        $appid  = "wx9883df9ffef003c0";
        $secret = "8729c142900b3d0bbe8104bc7a1b2aae";

        //这里获取到了code
        $code   = $_GET['code'];

        //第一步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
        $oauth2 = $this->http_curl($oauth2Url);

        //accestoken
        $access_token = $oauth2["access_token"];
        //openid
        $openid = $oauth2['openid'];


        //第二步:根据全局access_token和openid查询用户信息
        $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $userinfo = $this->http_curl($get_user_info_url);

        $user     = [
            'openid'    => $userinfo['openid'],
            'nickname'  => $userinfo['nickname'],
            'head_url'  => $userinfo['headimgurl'],
        ];

        $userm     = model('users')->where(['openid'=>$user['openid']])->find();

        if(!empty($userm)){
            //如果有 修改昵称头像
            $userm->nickname    = $userinfo['nickname'];
            $user->head_url     = $userinfo['headimgurl'];
            $userm->save();
        }else{
            //无则新建用户
            model('users')->isUpdate(false)->save($user);
        }
        return $this->returnData('200','请求成功',$user);

    }

    /**
     * @param $url
     * @return mixed
     */
    function http_curl($url){
        //用curl传参
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        //关闭ssl验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);


        curl_setopt($ch,CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

}
